<?php

namespace app\common\service\ai;

use think\facade\Db;
use app\common\service\CommonService;

/**
 * 分销员
 */
class PromoterService extends CommonService {

    /**
     * 表名
     * @static string
     */
    protected static $table = 'promoter';
    
    /**
     * 列表
     * @param array $data 数据
     * @return array
     */
    public static function getListByAdmin($data) {
        $list = self::query(self::$table)
                ->leftJoin('member m', 'm.member_id = a.member_id')
                ->_like('m.nickname#keyword,m.username#keyword,m.phone#keyword')
                ->_dateBetween('a.create_time#create_time')
                ->where(['a.pid' => 0])
                ->field('a.*,m.nickname,m.username,m.phone')
                ->_page(true);
        return $list;
    }
    
    
    
    /**
     * 分销数据列表
     * @param array $data 数据
     * @return array
     */
    public static function getCommissionListByAdmin($data) {
        $list = self::query('promoter_commission')
//            ->leftJoin('order o', 'o.order_no = a.order_no')
            ->leftJoin('member m', 'm.member_id = a.member_id')
            ->leftJoin('member m2', 'm2.member_id = a.promoter_mid')
            ->_orLike('a.order#keyword,m.nickname#keyword,m.username#keyword,m.phone#keyword')
            ->_dateBetween('a.create_time#create_time')
            ->where(['a.promoter_mid' => $data['promoter_mid']])
			->where(['o.status' => 1])
//            ->where(['o.type' => 1])
            ->order('a.id desc')
            ->field('a.*,m.phone,m2.phone promoter_phone,a.promoter_mid')    
            ->_page(true);
        return $list;
    }
    
    /**
     * 分销数据列表
     * @param array $data 数据
     * @return array
     */
    public static function getCommissionList($data) {
        $list = self::query('promoter_commission')
            ->leftJoin('order o', 'o.order_no = a.order_no')
            ->leftJoin('member m', 'm.member_id = a.member_id')
            ->leftJoin('member m2', 'm2.member_id = a.promoter_mid')
            ->_orLike('o.order#keyword')
            ->_dateBetween('a.create_time#create_time')
            ->where(['a.promoter_mid' => $data['member_id']])
            ->where(['o.status' => 1])
//            ->where(['o.type' => 1])
            ->order('a.id desc')
            ->field('a.*')    
            ->_page();
        return $list;
    }
    
    /**
     * 新增
     * @param array $data 数据
     * @return array
     */
    public static function add($data) {
        $promoter = self::query(self::$table)->where(['member_id' => $data['member_id'], 'pid' => 0, 'is_delete' => 0])->_find();
        $promoter && self::error('已是分销员');
        $result = self::model()->_insert(self::$table, $data, false);
        $result ? self::success() : self::error();
    }
    
    /**
     * 绑定
     * @param array $data 数据
     * @return array
     */
    public static function bind($data) {
        $bind_promoter = self::query(self::$table)->where(['member_id' => $data['member_id'], 'promoter_mid' => $data['promoter_mid'], 'is_delete' => 0])->_find();
        $bind_promoter && self::error('已绑定分销员');
        $promoter = self::query(self::$table)->where(['member_id' => $data['promoter_mid'], 'pid' => 0, 'is_delete' => 0])->_find();
        !$promoter && self::error('分销员不存在');
        $promoter_data = [
            'pid' => $promoter['id'],
            'promoter_mid' => $data['promoter_mid'],
            'member_id' => $data['member_id'],
        ];
        $result = self::model()->_insert(self::$table, $promoter_data, false);
        $result ? self::success() : self::error();
    }
    
    /**
     * 解绑
     * @param array $data 数据
     * @return array
     */
    public static function unbind($data) {
        $bind_promoter = self::query(self::$table)->where(['member_id' => $data['member_id'], 'promoter_mid' => $data['promoter_mid'], 'is_delete' => 0])->_find();
    }
    
    /**
     * 是否分销员(0否,1是)
     * @param int $member_id 会员ID
     * @return array
     */
    public static function isPromoter($member_id) {
        return self::query(self::$table)->where(['member_id' => $member_id, 'pid' => 0, 'is_delete' => 0])->_find() ? 1 : 0;
    }
    
    /**
     * 创建分佣
     * @param string $order_no 订单号
     * @return array
     */
    public static function createCommission($order_no) {
        $order = self::query('order')->where(['order_no' => $order_no])->_find();
        if (!$order) {
             return self::error_out('订单不存在');
        }
        $promoter_mid = self::query('member')->where(['member_id' => $order['member_id']])->_value('share_id');
        if (!$promoter_mid) {
             return self::error_out('会员不存在');
        }
        $promoter = self::query(self::$table)->where(['member_id' => $promoter_mid, 'pid' => 0, 'is_delete' => 0])->_find();
        if (!$promoter) {
             return self::error_out('不是分销员');
        }
        $rate = SystemConfigService::getDetail('COMMISSION_RATE')['value'] ?? 0;
        $commission = $order['sale_price'] * $rate;
        $data = [
            'order_no' => $order_no,
            'member_id' => $order['member_id'],
            'promoter_mid' => $promoter_mid,
            'rate' => $rate,
            'commission' => $commission,
        ];
        $result = self::model()->_insert('promoter_commission', $data, false);
        return $result ? self::success_out() : self::error_out();
    }

}
